#coding:utf-8
from pyquery import PyQuery as pq
from param import getCookies,URL,DATA
import requests
import os
#写入html
def writeToHtml(respText,i):
 path=r"D:\html\test"+str(i)+".html"
 fo = open(path, "w+")
 fo.write(respText.encode("gbk"))
 fo.close()
 print "writen to",path

def getText(startDate,endDate):
  url=URL
  data=DATA
  cookies=getCookies()
  data['ctl00$ContentPlaceHolder1$OtherAscx1$sDateTime']=startDate
  data['ctl00$ContentPlaceHolder1$OtherAscx1$eDateTime']=endDate
  print data['ctl00$ContentPlaceHolder1$OtherAscx1$sDateTime']+"-"+data['ctl00$ContentPlaceHolder1$OtherAscx1$eDateTime']
  resp=requests.post(url,cookies=cookies,data=data)
  return resp.text

def writeToText(path,dest):
 fo = open(dest, "w+")
 for fpathe,dirs,fs in os.walk(path):
  for f in fs:
    html = os.path.join(fpathe,f)
    d=pq(filename=html,encoding="gbk")
    table=d('table').eq(4)
    for i in range(2,10):
       tr=table('tr').eq(i)
       line=tr('td').eq(0).text()+"\t"+tr('td').eq(1).text()+"\t"+tr('td').eq(5).text()+"\t"+tr('td').eq(10).text()+"\n"
       line=line.encode('utf-8')
       if line.isspace():
           continue
       fo.write(line)
 fo.close()